﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.Core.Model;
using Wolfy.NetDisk.Core.Repository;
using Wolfy.NetDisk.Core.Security;
using Wolfy.NetDisk.Core.Service;

namespace Wolfy.NetDisk.Service
{
    public class UserService : CRUDService<User>, IUserService
    {
        public UserService(IRepository<User> repository)
            : base(repository)
        { }
        /// <summary>
        /// 用户名是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsExist(string name)
        {
            return repository.Where(o => o.UserName == name, true).Any();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        public void ChangePassword(int id, string password)
        {
            repository.Get(id).Pwd = AesSecret.EncryptToken(password);
            repository.Save();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User Login(string name, string password)
        {
            var user = repository.Where(o => o.UserName == name && o.IsDeleted == false).FirstOrDefault();
            if (user == null || user.Pwd != AesSecret.EncryptToken(password))
            {
                return null;
            }
            return user;
        }
    }
}
